import {
  valueAddedGet,
  queryProductLineList,
  queryOriginalFactoryList,
  newspaperDateConfigList,
  addNewspaperDate,
  editNewspaperDate,
} from '@api/system_config_v1.js';
export default {
  data() {
    return {
      params: {
        isMust: 1
      },
      originalFactoryList: [],
      productLineList: [],
      originalFactoryMap: {},
      productLineMap: {},
      checkList: [],
      selectList: [],
      orderTypeList: [],
      loading: false,
      switchValue: false,
      attributeType: 1,
      orderTypeInfo: [],
      optionList: [],
      transferData: [],
      transferValue: []
    }
  },
  created() {
    this.initData()
    this.initGlobalBtn()
    this.queryProductLineCode = this.$route.query.queryProductLineCode
    this.queryProductLineCode && this.getInfo()
  },
  methods: {
    initData () {
      this.newspaperEndDayList = new Array(31).fill('').map((x, i) => {return {label: `下月${i + 1}日`, value: i + 1}})
    },
    async getInfo () {
      this.initGlobalBtn()
      let data = {}
      await this.$axios.$post(newspaperDateConfigList, {
        productLineCode: this.queryProductLineCode,
        newspaperDateConfigType: 1
      }).then(({payload}) => {
        data = payload.content[0]
      })
      let obj = {
        productLineName: data.productLineName,
        productLineCode: data.productLineCode,
        originalFactoryCode: data.originalFactoryCode,
        configSaleDateId: data.configSaleDateId,
        newspaperEndDay: Number(data.newspaperEndDay)
      }
      this.queryOriginalFactoryList(data.originalFactoryName)
      await this.queryProductLineList()
      this.transferValue = [data.productLineCode]
      this.params = obj
    },
    initGlobalBtn() {
      let btnObj = {
        type: 'btn',
        arr: [{ type: '', val: '返回', handle: this.goBack }]
      };
      let submitBtn = {
        type: 'primary',
        val: this.queryProductLineCode ? '修改' : '提交',
        handle: this.onSubmit
      };
      btnObj.arr.push(submitBtn);
      this.$store.commit('changeLayoutHandle', btnObj);
    },
    goBack () {
      this.$router.push({
        path: '/system-config/announcement-date-configuration',
        query: {
          type: 'fixed'
        }
      })
    },
    onSubmit () {
      let next = true
      let msg = ''
      if (!this.params.originalFactoryCode) {
        if (next) {
          next = false
          msg = '请选择原厂'
        }
      }
      if (!this.transferValue.length) {
        if (next) {
          next = false
          msg = '请选择产品线'
        }
      }
      if (!this.params.newspaperEndDay) {
        if (next) {
          next = false
          msg = '请选择跨月报台截止日期'
        }
      }
      if (!next) return this.$message.warning(msg)
      // valueRange
      let sendData = {
        newspaperDateType: 1,
        newspaperMonth: 2,
        newspaperEndDay: this.params.newspaperEndDay,
        originalFactoryCode: this.params.originalFactoryCode,
        originalFactoryName: this.originalFactoryMap[this.params.originalFactoryCode],
        productLineInfoList: []
      }
      this.transferValue.forEach(item => {
        sendData.productLineInfoList.push({
          productLineName: this.productLineMap[item],
          productLineCode: item
        })
      })
      let ex = addNewspaperDate
      let sendMsg = '添加成功'
      if (this.queryProductLineCode) {
        ex = editNewspaperDate
        sendData.configSaleDateId = this.params.configSaleDateId
        sendMsg = '修改成功'
      }
      console.log(sendData, '<sendData')
      this.$axios.$post(ex, sendData).then(res => {
        this.$message.success(sendMsg)
        this.goBack()
      });
    },
    // 原厂下拉
    queryOriginalFactoryList(val) {
      if (val !== '') {
        this.loading = true;
        let query = {
          originalFactoryName: val,
        };
        this.$axios.$post(queryOriginalFactoryList, query).then(res => {
          this.setOriginList(res.payload, 'originalFactoryList', 'originalFactoryMap')
          this.loading = false;
        });
      }
    },
    // 产品线下拉
    queryProductLineList (val) {
      this.params.productLineCode = []
      if (val !== '') {
        this.loading = true;
        let query = {
          originalFactoryCode: this.params.originalFactoryCode,
          productLineName: val
        };
        return this.$axios.$post(queryProductLineList, query).then(res => {
          this.productLineList = res.payload
          this.transferValue = []
          this.transferData = []
          res.payload.forEach(item => {
            this.productLineMap[item.productLineCode] = item.productLineName
            this.transferData.push({
              key: item.productLineCode,
              label: item.productLineName
            })
          })
          this.loading = false;
        });
      }
    },
    setOriginList (data, key, mapKey) {
      this[key] = data
      data.forEach(item => {
        this[mapKey][item.originalFactoryCode] = item.originalFactoryName
      })
    }
  }
};
